//
//  HZLoginView.m
//  微博 OC
//
//  Created by 黑眼圈 on 16/4/18.
//  Copyright © 2016年 heiyan. All rights reserved.
//

#import "HZLoginView.h"

//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS

#import <Masonry.h>
#import "UILabel+HZSettingLabel.h"

@interface HZLoginView()

@property (nonatomic,strong)UIImageView *iconView;
@property (nonatomic,strong)UIImageView *translucenceView;
@property (nonatomic,strong)UIImageView *rotateView;

@property (nonatomic,strong)UILabel *titleLabel;

@property(nonatomic,strong)UIButton*  registerBtn;
@property(nonatomic,strong)UIButton*  loginBtn;

@end


@implementation HZLoginView


-(void)setImg:(NSString*)imgName :(NSString*)title{
    
  
    
    if (imgName != nil) {
        
        self.iconView.image = [UIImage imageNamed:imgName];
        self.titleLabel.text = title;
        self.rotateView.hidden = YES;
        return;
    }else{
        
        self.rotateView.hidden = NO;
        
  
        //开启旋转动画
        CABasicAnimation* basicAn = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        basicAn.duration = 10;
        basicAn.toValue = @(2*M_PI);
        basicAn.repeatCount = MAXFLOAT;
        
        //切换控制器时不移除动画
        basicAn.removedOnCompletion = NO;
        
        [self.rotateView.layer addAnimation:basicAn forKey:nil];
        
    
        
        
        
    }
    
    
    
}




- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //设置背景色
        self.backgroundColor = [UIColor colorWithWhite:237/255.0 alpha:1];
        
        //添加子控件
        [self setupUI];
        
    }
    return self;
}


#pragma mark -
#pragma mark     实现添加子控件
-(void)setupUI{
    
     [self addSubview:self.rotateView];
     [self addSubview:self.translucenceView];
    [self addSubview:self.iconView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.registerBtn];
    [self addSubview:self.loginBtn];
    
    //约束
    [self.rotateView makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(self.center);
    }];
    
    
    [self.iconView makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(self.center);
    }];
    
    [self.translucenceView makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(self.center);
    }];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.iconView.centerX);
        make.top.equalTo(self.translucenceView.bottom).offset(10);
        
    }];
    
    [self.registerBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.titleLabel);
        make.width.equalTo(100);
        make.top.equalTo(self.titleLabel.bottom).offset(10);
    }];
    
    [self.loginBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.equalTo(self.titleLabel.trailing);
        make.width.equalTo(100);
        make.top.equalTo(self.titleLabel.bottom).offset(10);
    }];
    
}

-(UIImageView *)iconView{
    if(_iconView  == nil){
        _iconView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"visitordiscover_feed_image_house"]];
        [_iconView sizeToFit];
    }
    return _iconView;
}
-(UIImageView *)translucenceView{
    if(_translucenceView  == nil){
        _translucenceView = [[UIImageView alloc]initWithImage: [UIImage imageNamed: @"visitordiscover_feed_mask_smallicon"]];
        [_translucenceView sizeToFit];
    }
    return _translucenceView;
}
-(UIImageView *)rotateView{
    if(_rotateView  == nil){
        _rotateView = [[UIImageView alloc]initWithImage: [UIImage imageNamed: @"visitordiscover_feed_image_smallicon"]];
        [_rotateView sizeToFit];
    }
    return _rotateView;
}

-(UILabel *)titleLabel{
    if(_titleLabel == nil){
 
        _titleLabel = [[UILabel alloc]initWithTitle:@"关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜" :[UIColor grayColor] :230 :14];
  
    }
    return _titleLabel;
}

-(UIButton*)loginBtn{
    if (_loginBtn == nil) {
        
        _loginBtn = [[UIButton alloc]init];
        
        //按钮点击事件
        [_loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
        
        
        UIImage* oldImg = [UIImage imageNamed:@"searchbar_textfield_background"];
        
        CGFloat w=oldImg.size.width*0.5;
        CGFloat h=oldImg.size.height*0.5;
        
        //切图
        UIImage* newImg = [oldImg resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w)];
        
        [_loginBtn setBackgroundImage:newImg forState:UIControlStateNormal];
        
           [_loginBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        
    }
    return _loginBtn;

}


-(UIButton*)registerBtn{
    if (_registerBtn == nil) {
        
        _registerBtn = [[UIButton alloc]init];
        
        //按钮点击事件
        [_registerBtn addTarget:self action:@selector(registered) forControlEvents:UIControlEventTouchUpInside];
    
        
        UIImage* oldImg = [UIImage imageNamed:@"searchbar_textfield_background"];
       
        CGFloat w=oldImg.size.width*0.5;
        CGFloat h=oldImg.size.height*0.5;
        
        //切图
        UIImage* newImg = [oldImg resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w)];
        
        [_registerBtn setBackgroundImage:newImg forState:UIControlStateNormal];
        
        
        [_registerBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        _registerBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        
    }
    return _registerBtn;
}


#pragma mark -
#pragma mark     按钮点击事件
-(void)login{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:self];
}



-(void)registered{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"register" object:self];
    
}





@end
